<template>
  <XtxDialog title="添加收货地址" v-model:visible="dialogVisible">
    <div class="xtx-form">
      <div class="xtx-form-item">
        <div class="label">收货人：</div>
        <div class="field">
          <input class="input" v-model="formData.receiver" placeholder="请输入收货人" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">手机号：</div>
        <div class="field">
          <input class="input" v-model="formData.contact" placeholder="请输入手机号" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">地区：</div>
        <div class="field">
          <XtxCity @change="getCityData" :fullLocation="formData.fullLocation"
            :placeholder="formData.fullLocation ? '' : '请选择所在地区'" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">详细地址：</div>
        <div class="field">
          <input class="input" v-model="formData.address" placeholder="请输入详细地址" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">邮政编码：</div>
        <div class="field">
          <input class="input" v-model="formData.postalCode" placeholder="请输入邮政编码" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">地址标签：</div>
        <div class="field">
          <input class="input" v-model="formData.addressTags" placeholder="请输入地址标签，逗号分隔" />
        </div>
      </div>
    </div>
    <template v-slot:footer>
      <XtxButton @click="dialogVisible = false" type="gray" style="margin-right:20px">取消</XtxButton>
      <XtxButton @click="submitAddress" type="primary">确认</XtxButton>
    </template>
  </XtxDialog>
</template>
<script>
import { ref, reactive } from 'vue'
import { addAddress, editAddress } from '@/api/order'
import Message from '@/components/library/Message'
export default {
  name: 'AddressEdit',
  setup(props, { emit }) {
    const dialogVisible = ref(false)
    // 打开对话框的函数
    const open = (address) => {
      // 打开层时表单数据置空
      for (const key in formData) {
        if (key !== 'isDefault') {
          formData[key] = ''
        }
      }
      dialogVisible.value = true
      for (const key in address) {
        formData[key] = address[key]
      }
    }
    // 表单数据
    const formData = reactive({
      id: null, // 修改时是需要id
      receiver: '',
      contact: '',
      provinceCode: '',
      cityCode: '',
      countyCode: '',
      address: '',
      postalCode: '',
      addressTags: '',
      isDefault: 1,
      fullLocation: ''
    })
    // 获取城市组件的数据
    const getCityData = (data) => {
      formData.provinceCode = data.provinceCode
      formData.cityCode = data.cityCode
      formData.countyCode = data.countyCode
      formData.fullLocation = data.fullLocation
    }
    // 提交添加的数据
    const submitAddress = () => {
      // 1.进行表单数据的验证
      if (formData.id) {
        // 调修改收货地址的api
        editAddress(formData).then(data => {
          emit('edit-address', formData)
          dialogVisible.value = false
          Message({ type: 'success', text: '修改收货地址成功' })
        })
      } else {
        // 2.调添加收货地址的api
        addAddress(formData).then(data => {
          // 把当前新增的收货信息和原来的收货地址进行合并,这样不需要刷新页面就可以拿到最新的地址信息
          // 添加上id字段
          formData.id = data.result.id
          emit('edit-address', formData)
          dialogVisible.value = false
          Message({ type: 'success', text: '添加收货地址成功' })
        })
      }
    }

    return { dialogVisible, open, formData, getCityData, submitAddress }
  }
}
</script>
<style scoped lang="less">
.xtx-dialog {
  :deep(.wrapper) {
    width: 780px;

    .body {
      font-size: 14px;
    }
  }
}

.xtx-form {
  padding: 0;

  input {
    outline: none;

    &::placeholder {
      color: #ccc;
    }
  }

  .xtx-city {
    width: 320px;

    :deep(.select) {
      height: 50px;
      line-height: 48px;
      display: flex;
      padding: 0 10px;
      justify-content: space-between;

      .placeholder {
        color: #ccc;
      }

      i {
        color: #ccc;
        font-size: 18px;
      }

      .value {
        font-size: 14px;
      }
    }

    :deep(.option) {
      top: 49px;
    }
  }
}
</style>